package merge_sort;

public class merge {
	static int[] Merge_Sort(int[] massive)
    {
        if (massive.length <= 1)
            return massive;
        int mid_point = massive.length / 2;
        return Merge(Merge_Sort(java.util.Arrays.copyOfRange(massive, 0, mid_point-1)), Merge_Sort(java.util.Arrays.copyOfRange(massive, mid_point, massive.length-1)));
    }

    static int[] Merge(int[] mass1, int[] mass2)
    {
    	int a = 0, b = 0;
        int[] merged = new int[mass1.length + mass2.length];
        for (int i = 0; i < mass1.length + mass2.length; i++)
        {
            if (b < mass2.length && a < mass1.length)
                if (mass1[a] > mass2[b] && b < mass2.length)
                    merged[i] = mass2[b++];
                else 
                    merged[i] = mass1[a++];
            else
                if (b < mass2.length)
                    merged[i] = mass2[b++];
                else
                    merged[i] = mass1[a++];
        }
        return merged;
    }
}
